
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>变量声明 · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Interfaces.html" />
    
    
    <link rel="prev" href="Basic Types.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="tutorials/">
            
                <a href="tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="tutorials/TypeScript in 5 minutes.html">
            
                <a href="tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="tutorials/ASP.NET Core.html">
            
                <a href="tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="tutorials/ASP.NET 4.html">
            
                <a href="tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="tutorials/Gulp.html">
            
                <a href="tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="tutorials/Knockout.html">
            
                <a href="tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="tutorials/React & Webpack.html">
            
                <a href="tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="tutorials/React.html">
            
                <a href="tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="tutorials/Angular 2.html">
            
                <a href="tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="tutorials/Migrating from JavaScript.html">
            
                <a href="tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Basic Types.html">
            
                <a href="Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="Variable Declarations.html">
            
                <a href="Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Interfaces.html">
            
                <a href="Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="Classes.html">
            
                <a href="Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="Functions.html">
            
                <a href="Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="Generics.html">
            
                <a href="Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="Enums.html">
            
                <a href="Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="Type Inference.html">
            
                <a href="Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="Type Compatibility.html">
            
                <a href="Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="Advanced Types.html">
            
                <a href="Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="Symbols.html">
            
                <a href="Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="Iterators and Generators.html">
            
                <a href="Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="Modules.html">
            
                <a href="Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="Namespaces.html">
            
                <a href="Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="Namespaces and Modules.html">
            
                <a href="Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="Module Resolution.html">
            
                <a href="Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="Declaration Merging.html">
            
                <a href="Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="Writing Definition Files.html">
            
                <a href="Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="JSX.html">
            
                <a href="JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="Decorators.html">
            
                <a href="Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="Mixins.html">
            
                <a href="Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="Triple-Slash Directives.html">
            
                <a href="Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="Type Checking JavaScript Files.html">
            
                <a href="Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="declaration files/Introduction.html">
            
                <a href="declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="declaration files/Library Structures.html">
            
                <a href="declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="declaration files/Do's and Don'ts.html">
            
                <a href="declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="declaration files/By Example.html">
            
                <a href="declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="declaration files/Deep Dive.html">
            
                <a href="declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="declaration files/Publishing.html">
            
                <a href="declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="declaration files/Consumption.html">
            
                <a href="declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Project References.html">
            
                <a href="Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Typings for NPM Packages.html">
            
                <a href="Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Compiler Options.html">
            
                <a href="Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Configuring Watch.html">
            
                <a href="Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Compiler Options in MSBuild.html">
            
                <a href="Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="Integrating with Build Tools.html">
            
                <a href="Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Nightly Builds.html">
            
                <a href="Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../release-notes/">
            
                <a href="../release-notes/">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../release-notes/TypeScript 3.1.html">
            
                <a href="../release-notes/TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../release-notes/TypeScript 3.0.html">
            
                <a href="../release-notes/TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../release-notes/TypeScript 2.9.html">
            
                <a href="../release-notes/TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../release-notes/TypeScript 2.8.html">
            
                <a href="../release-notes/TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../release-notes/TypeScript 2.7.html">
            
                <a href="../release-notes/TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../release-notes/TypeScript 2.6.html">
            
                <a href="../release-notes/TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../release-notes/TypeScript 2.5.html">
            
                <a href="../release-notes/TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../release-notes/TypeScript 2.4.html">
            
                <a href="../release-notes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../release-notes/TypeScript 2.3.html">
            
                <a href="../release-notes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../release-notes/TypeScript 2.2.html">
            
                <a href="../release-notes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="../release-notes/TypeScript 2.1.html">
            
                <a href="../release-notes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="../release-notes/TypeScript 2.0.html">
            
                <a href="../release-notes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="../release-notes/TypeScript 1.8.html">
            
                <a href="../release-notes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="../release-notes/TypeScript 1.7.html">
            
                <a href="../release-notes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="../release-notes/TypeScript 1.6.html">
            
                <a href="../release-notes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="../release-notes/TypeScript 1.5.html">
            
                <a href="../release-notes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="../release-notes/TypeScript 1.4.html">
            
                <a href="../release-notes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="../release-notes/TypeScript 1.3.html">
            
                <a href="../release-notes/TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="../release-notes/TypeScript 1.1.html">
            
                <a href="../release-notes/TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >变量声明</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x53D8;&#x91CF;&#x58F0;&#x660E;">&#x53D8;&#x91CF;&#x58F0;&#x660E;</h1>
<p><code>let</code>&#x548C;<code>const</code>&#x662F;JavaScript&#x91CC;&#x76F8;&#x5BF9;&#x8F83;&#x65B0;&#x7684;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x65B9;&#x5F0F;&#x3002;
&#x50CF;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x63D0;&#x5230;&#x8FC7;&#x7684;&#xFF0C;<code>let</code>&#x5728;&#x5F88;&#x591A;&#x65B9;&#x9762;&#x4E0E;<code>var</code>&#x662F;&#x76F8;&#x4F3C;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x5927;&#x5BB6;&#x907F;&#x514D;&#x5728;JavaScript&#x91CC;&#x5E38;&#x89C1;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#x3002;
<code>const</code>&#x662F;&#x5BF9;<code>let</code>&#x7684;&#x4E00;&#x4E2A;&#x589E;&#x5F3A;&#xFF0C;&#x5B83;&#x80FD;&#x963B;&#x6B62;&#x5BF9;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x518D;&#x6B21;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x56E0;&#x4E3A;TypeScript&#x662F;JavaScript&#x7684;&#x8D85;&#x96C6;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x672C;&#x8EAB;&#x5C31;&#x652F;&#x6301;<code>let</code>&#x548C;<code>const</code>&#x3002;
&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4F1A;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x8FD9;&#x4E9B;&#x65B0;&#x7684;&#x58F0;&#x660E;&#x65B9;&#x5F0F;&#x4EE5;&#x53CA;&#x4E3A;&#x4EC0;&#x4E48;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x6765;&#x4EE3;&#x66FF;<code>var</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x4E4B;&#x524D;&#x4F7F;&#x7528;JavaScript&#x65F6;&#x6CA1;&#x6709;&#x7279;&#x522B;&#x5728;&#x610F;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x8282;&#x5185;&#x5BB9;&#x4F1A;&#x5524;&#x8D77;&#x4F60;&#x7684;&#x56DE;&#x5FC6;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x5DF2;&#x7ECF;&#x5BF9;<code>var</code>&#x58F0;&#x660E;&#x7684;&#x602A;&#x5F02;&#x4E4B;&#x5904;&#x4E86;&#x5982;&#x6307;&#x638C;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x53EF;&#x4EE5;&#x8F7B;&#x677E;&#x5730;&#x7565;&#x8FC7;&#x8FD9;&#x8282;&#x3002;</p>
<h1 id="var-&#x58F0;&#x660E;"><code>var</code> &#x58F0;&#x660E;</h1>
<p>&#x4E00;&#x76F4;&#x4EE5;&#x6765;&#x6211;&#x4EEC;&#x90FD;&#x662F;&#x901A;&#x8FC7;<code>var</code>&#x5173;&#x952E;&#x5B57;&#x5B9A;&#x4E49;JavaScript&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;
</code></pre>
<p>&#x5927;&#x5BB6;&#x90FD;&#x80FD;&#x7406;&#x89E3;&#xFF0C;&#x8FD9;&#x91CC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>a</code>&#x503C;&#x4E3A;<code>10</code>&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> message = <span class="hljs-string">&quot;Hello, world!&quot;</span>;

    <span class="hljs-keyword">return</span> message;
}
</code></pre>
<p>&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x5176;&#x5B83;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x8BBF;&#x95EE;&#x76F8;&#x540C;&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">g</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> b = a + <span class="hljs-number">1</span>;
        <span class="hljs-keyword">return</span> b;
    }
}

<span class="hljs-keyword">var</span> g = f();
g(); <span class="hljs-comment">// returns 11;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;<code>g</code>&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;<code>f</code>&#x51FD;&#x6570;&#x91CC;&#x5B9A;&#x4E49;&#x7684;<code>a</code>&#x53D8;&#x91CF;&#x3002;
&#x6BCF;&#x5F53;<code>g</code>&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x90FD;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;<code>f</code>&#x91CC;&#x7684;<code>a</code>&#x53D8;&#x91CF;&#x3002;
&#x5373;&#x4F7F;&#x5F53;<code>g</code>&#x5728;<code>f</code>&#x5DF2;&#x7ECF;&#x6267;&#x884C;&#x5B8C;&#x540E;&#x624D;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x5B83;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x53CA;&#x4FEE;&#x6539;<code>a</code>&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> a = <span class="hljs-number">1</span>;

    a = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">var</span> b = g();
    a = <span class="hljs-number">3</span>;

    <span class="hljs-keyword">return</span> b;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">g</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> a;
    }
}

f(); <span class="hljs-comment">// returns 2</span>
</code></pre>
<h2 id="&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;">&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;</h2>
<p>&#x5BF9;&#x4E8E;&#x719F;&#x6089;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x7684;&#x4EBA;&#x6765;&#x8BF4;&#xFF0C;<code>var</code>&#x58F0;&#x660E;&#x6709;&#x4E9B;&#x5947;&#x602A;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;&#x3002;
&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">shouldInitialize: <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">if</span> (shouldInitialize) {
        <span class="hljs-keyword">var</span> x = <span class="hljs-number">10</span>;
    }

    <span class="hljs-keyword">return</span> x;
}

f(<span class="hljs-literal">true</span>);  <span class="hljs-comment">// returns &apos;10&apos;</span>
f(<span class="hljs-literal">false</span>); <span class="hljs-comment">// returns &apos;undefined&apos;</span>
</code></pre>
<p>&#x6709;&#x4E9B;&#x8BFB;&#x8005;&#x53EF;&#x80FD;&#x8981;&#x591A;&#x770B;&#x51E0;&#x904D;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;
&#x53D8;&#x91CF;<code>x</code>&#x662F;&#x5B9A;&#x4E49;&#x5728;<em><code>if</code>&#x8BED;&#x53E5;&#x91CC;&#x9762;</em>&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x5374;&#x53EF;&#x4EE5;&#x5728;&#x8BED;&#x53E5;&#x7684;&#x5916;&#x9762;&#x8BBF;&#x95EE;&#x5B83;&#x3002;
&#x8FD9;&#x662F;&#x56E0;&#x4E3A;<code>var</code>&#x58F0;&#x660E;&#x53EF;&#x4EE5;&#x5728;&#x5305;&#x542B;&#x5B83;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x6A21;&#x5757;&#xFF0C;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x6216;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x90E8;&#x4EFB;&#x4F55;&#x4F4D;&#x7F6E;&#x88AB;&#x8BBF;&#x95EE;&#xFF08;&#x6211;&#x4EEC;&#x540E;&#x9762;&#x4F1A;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#xFF09;&#xFF0C;&#x5305;&#x542B;&#x5B83;&#x7684;&#x4EE3;&#x7801;&#x5757;&#x5BF9;&#x6B64;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x5F71;&#x54CD;&#x3002;
&#x6709;&#x4E9B;&#x4EBA;&#x79F0;&#x6B64;&#x4E3A;<em><code>var</code>&#x4F5C;&#x7528;&#x57DF;</em>&#x6216;<em>&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;</em>&#x3002;
&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4E5F;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<p>&#x8FD9;&#x4E9B;&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;&#x53EF;&#x80FD;&#x4F1A;&#x5F15;&#x53D1;&#x4E00;&#x4E9B;&#x9519;&#x8BEF;&#x3002;
&#x5176;&#x4E2D;&#x4E4B;&#x4E00;&#x5C31;&#x662F;&#xFF0C;&#x591A;&#x6B21;&#x58F0;&#x660E;&#x540C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x5E76;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sumMatrix</span>(<span class="hljs-params">matrix: <span class="hljs-built_in">number</span>[][]</span>) </span>{
    <span class="hljs-keyword">var</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; matrix.length; i++) {
        <span class="hljs-keyword">var</span> currentRow = matrix[i];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; currentRow.length; i++) {
            sum += currentRow[i];
        }
    }

    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x5F88;&#x5BB9;&#x6613;&#x770B;&#x51FA;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#xFF0C;&#x91CC;&#x5C42;&#x7684;<code>for</code>&#x5FAA;&#x73AF;&#x4F1A;&#x8986;&#x76D6;&#x53D8;&#x91CF;<code>i</code>&#xFF0C;&#x56E0;&#x4E3A;&#x6240;&#x6709;<code>i</code>&#x90FD;&#x5F15;&#x7528;&#x76F8;&#x540C;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x7684;&#x53D8;&#x91CF;&#x3002;
&#x6709;&#x7ECF;&#x9A8C;&#x7684;&#x5F00;&#x53D1;&#x8005;&#x4EEC;&#x5F88;&#x6E05;&#x695A;&#xFF0C;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x53EF;&#x80FD;&#x5728;&#x4EE3;&#x7801;&#x5BA1;&#x67E5;&#x65F6;&#x6F0F;&#x6389;&#xFF0C;&#x5F15;&#x53D1;&#x65E0;&#x7A77;&#x7684;&#x9EBB;&#x70E6;&#x3002;</p>
<h2 id="&#x6355;&#x83B7;&#x53D8;&#x91CF;&#x602A;&#x5F02;&#x4E4B;&#x5904;">&#x6355;&#x83B7;&#x53D8;&#x91CF;&#x602A;&#x5F02;&#x4E4B;&#x5904;</h2>
<p>&#x5FEB;&#x901F;&#x7684;&#x731C;&#x4E00;&#x4E0B;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x8FD4;&#x56DE;&#x4EC0;&#x4E48;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-built_in">console</span>.log(i); }, <span class="hljs-number">100</span> * i);
}
</code></pre>
<p>&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#xFF0C;<code>setTimeout</code>&#x4F1A;&#x5728;&#x82E5;&#x5E72;&#x6BEB;&#x79D2;&#x7684;&#x5EF6;&#x65F6;&#x540E;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF08;&#x7B49;&#x5F85;&#x5176;&#x5B83;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#xFF09;&#x3002;</p>
<p>&#x597D;&#x5427;&#xFF0C;&#x770B;&#x4E00;&#x4E0B;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-text">10
10
10
10
10
10
10
10
10
10
</code></pre>
<p>&#x5F88;&#x591A;JavaScript&#x7A0B;&#x5E8F;&#x5458;&#x5BF9;&#x8FD9;&#x79CD;&#x884C;&#x4E3A;&#x5DF2;&#x7ECF;&#x5F88;&#x719F;&#x6089;&#x4E86;&#xFF0C;&#x4F46;&#x5982;&#x679C;&#x4F60;&#x5F88;&#x4E0D;&#x89E3;&#xFF0C;&#x4F60;&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x4EBA;&#x3002;
&#x5927;&#x591A;&#x6570;&#x4EBA;&#x671F;&#x671B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-text">0
1
2
3
4
5
6
7
8
9
</code></pre>
<p>&#x8FD8;&#x8BB0;&#x5F97;&#x6211;&#x4EEC;&#x4E0A;&#x9762;&#x63D0;&#x5230;&#x7684;&#x6355;&#x83B7;&#x53D8;&#x91CF;&#x5417;&#xFF1F;
&#x6211;&#x4EEC;&#x4F20;&#x7ED9;<code>setTimeout</code>&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x5B9E;&#x9645;&#x4E0A;&#x90FD;&#x5F15;&#x7528;&#x4E86;&#x76F8;&#x540C;&#x4F5C;&#x7528;&#x57DF;&#x91CC;&#x7684;&#x540C;&#x4E00;&#x4E2A;<code>i</code>&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x82B1;&#x70B9;&#x65F6;&#x95F4;&#x601D;&#x8003;&#x4E00;&#x4E0B;&#x8FD9;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x3002;
<code>setTimeout</code>&#x5728;&#x82E5;&#x5E72;&#x6BEB;&#x79D2;&#x540E;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x662F;&#x5728;<code>for</code>&#x5FAA;&#x73AF;&#x7ED3;&#x675F;&#x540E;&#x3002;
<code>for</code>&#x5FAA;&#x73AF;&#x7ED3;&#x675F;&#x540E;&#xFF0C;<code>i</code>&#x7684;&#x503C;&#x4E3A;<code>10</code>&#x3002;
&#x6240;&#x4EE5;&#x5F53;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B83;&#x4F1A;&#x6253;&#x5370;&#x51FA;<code>10</code>&#xFF01;</p>
<p>&#x4E00;&#x4E2A;&#x901A;&#x5E38;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;IIFE&#xFF09;&#x6765;&#x6355;&#x83B7;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x65F6;<code>i</code>&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    <span class="hljs-comment">// capture the current state of &apos;i&apos;</span>
    <span class="hljs-comment">// by invoking a function with its current value</span>
    (<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>) </span>{
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-built_in">console</span>.log(i); }, <span class="hljs-number">100</span> * i);
    })(i);
}
</code></pre>
<p>&#x8FD9;&#x79CD;&#x5947;&#x602A;&#x7684;&#x5F62;&#x5F0F;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x53F8;&#x7A7A;&#x89C1;&#x60EF;&#x4E86;&#x3002;
&#x53C2;&#x6570;<code>i</code>&#x4F1A;&#x8986;&#x76D6;<code>for</code>&#x5FAA;&#x73AF;&#x91CC;&#x7684;<code>i</code>&#xFF0C;&#x4F46;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x8D77;&#x4E86;&#x540C;&#x6837;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4E0D;&#x7528;&#x600E;&#x4E48;&#x6539;<code>for</code>&#x5FAA;&#x73AF;&#x4F53;&#x91CC;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<h1 id="let-&#x58F0;&#x660E;"><code>let</code> &#x58F0;&#x660E;</h1>
<p>&#x73B0;&#x5728;&#x4F60;&#x5DF2;&#x7ECF;&#x77E5;&#x9053;&#x4E86;<code>var</code>&#x5B58;&#x5728;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#xFF0C;&#x8FD9;&#x6070;&#x597D;&#x8BF4;&#x660E;&#x4E86;&#x4E3A;&#x4EC0;&#x4E48;&#x7528;<code>let</code>&#x8BED;&#x53E5;&#x6765;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x3002;
&#x9664;&#x4E86;&#x540D;&#x5B57;&#x4E0D;&#x540C;&#x5916;&#xFF0C;<code>let</code>&#x4E0E;<code>var</code>&#x7684;&#x5199;&#x6CD5;&#x4E00;&#x81F4;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> hello = <span class="hljs-string">&quot;Hello!&quot;</span>;
</code></pre>
<p>&#x4E3B;&#x8981;&#x7684;&#x533A;&#x522B;&#x4E0D;&#x5728;&#x8BED;&#x6CD5;&#x4E0A;&#xFF0C;&#x800C;&#x662F;&#x8BED;&#x4E49;&#xFF0C;&#x6211;&#x4EEC;&#x63A5;&#x4E0B;&#x6765;&#x4F1A;&#x6DF1;&#x5165;&#x7814;&#x7A76;&#x3002;</p>
<h2 id="&#x5757;&#x4F5C;&#x7528;&#x57DF;">&#x5757;&#x4F5C;&#x7528;&#x57DF;</h2>
<p>&#x5F53;&#x7528;<code>let</code>&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x5B83;&#x4F7F;&#x7528;&#x7684;&#x662F;<em>&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;</em>&#x6216;<em>&#x5757;&#x4F5C;&#x7528;&#x57DF;</em>&#x3002;
&#x4E0D;&#x540C;&#x4E8E;&#x4F7F;&#x7528;<code>var</code>&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x90A3;&#x6837;&#x53EF;&#x4EE5;&#x5728;&#x5305;&#x542B;&#x5B83;&#x4EEC;&#x7684;&#x51FD;&#x6570;&#x5916;&#x8BBF;&#x95EE;&#xFF0C;&#x5757;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x5728;&#x5305;&#x542B;&#x5B83;&#x4EEC;&#x7684;&#x5757;&#x6216;<code>for</code>&#x5FAA;&#x73AF;&#x4E4B;&#x5916;&#x662F;&#x4E0D;&#x80FD;&#x8BBF;&#x95EE;&#x7684;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">input: <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">let</span> a = <span class="hljs-number">100</span>;

    <span class="hljs-keyword">if</span> (input) {
        <span class="hljs-comment">// Still okay to reference &apos;a&apos;</span>
        <span class="hljs-keyword">let</span> b = a + <span class="hljs-number">1</span>;
        <span class="hljs-keyword">return</span> b;
    }

    <span class="hljs-comment">// Error: &apos;b&apos; doesn&apos;t exist here</span>
    <span class="hljs-keyword">return</span> b;
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;2&#x4E2A;&#x53D8;&#x91CF;<code>a</code>&#x548C;<code>b</code>&#x3002;
<code>a</code>&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x662F;<code>f</code>&#x51FD;&#x6570;&#x4F53;&#x5185;&#xFF0C;&#x800C;<code>b</code>&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x662F;<code>if</code>&#x8BED;&#x53E5;&#x5757;&#x91CC;&#x3002;</p>
<p>&#x5728;<code>catch</code>&#x8BED;&#x53E5;&#x91CC;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x4E5F;&#x5177;&#x6709;&#x540C;&#x6837;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">throw</span> <span class="hljs-string">&quot;oh no!&quot;</span>;
}
<span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Oh well.&quot;</span>);
}

<span class="hljs-comment">// Error: &apos;e&apos; doesn&apos;t exist here</span>
<span class="hljs-built_in">console</span>.log(e);
</code></pre>
<p>&#x62E5;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x53D8;&#x91CF;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x7279;&#x70B9;&#x662F;&#xFF0C;&#x5B83;&#x4EEC;&#x4E0D;&#x80FD;&#x5728;&#x88AB;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#x8BFB;&#x6216;&#x5199;&#x3002;
&#x867D;&#x7136;&#x8FD9;&#x4E9B;&#x53D8;&#x91CF;&#x59CB;&#x7EC8;&#x201C;&#x5B58;&#x5728;&#x201D;&#x4E8E;&#x5B83;&#x4EEC;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x91CC;&#xFF0C;&#x4F46;&#x5728;&#x76F4;&#x5230;&#x58F0;&#x660E;&#x5B83;&#x7684;&#x4EE3;&#x7801;&#x4E4B;&#x524D;&#x7684;&#x533A;&#x57DF;&#x90FD;&#x5C5E;&#x4E8E;<em>&#x6682;&#x65F6;&#x6027;&#x6B7B;&#x533A;</em>&#x3002;
&#x5B83;&#x53EA;&#x662F;&#x7528;&#x6765;&#x8BF4;&#x660E;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x5728;<code>let</code>&#x8BED;&#x53E5;&#x4E4B;&#x524D;&#x8BBF;&#x95EE;&#x5B83;&#x4EEC;&#xFF0C;&#x5E78;&#x8FD0;&#x7684;&#x662F;TypeScript&#x53EF;&#x4EE5;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-ts">a++; <span class="hljs-comment">// illegal to use &apos;a&apos; before it&apos;s declared;</span>
<span class="hljs-keyword">let</span> a;
</code></pre>
<p>&#x6CE8;&#x610F;&#x4E00;&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x62E5;&#x6709;&#x5757;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x88AB;&#x58F0;&#x660E;&#x524D;<em>&#x83B7;&#x53D6;</em>&#x5B83;&#x3002;
&#x53EA;&#x662F;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x5728;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x524D;&#x53BB;&#x8C03;&#x7528;&#x90A3;&#x4E2A;&#x51FD;&#x6570;&#x3002;
&#x5982;&#x679C;&#x751F;&#x6210;&#x4EE3;&#x7801;&#x76EE;&#x6807;&#x4E3A;ES2015&#xFF0C;&#x73B0;&#x4EE3;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF1B;&#x7136;&#x800C;&#xFF0C;&#x73B0;&#x4ECA;TypeScript&#x662F;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#x7684;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// okay to capture &apos;a&apos;</span>
    <span class="hljs-keyword">return</span> a;
}

<span class="hljs-comment">// &#x4E0D;&#x80FD;&#x5728;&apos;a&apos;&#x88AB;&#x58F0;&#x660E;&#x524D;&#x8C03;&#x7528;&apos;foo&apos;</span>
<span class="hljs-comment">// &#x8FD0;&#x884C;&#x65F6;&#x5E94;&#x8BE5;&#x629B;&#x51FA;&#x9519;&#x8BEF;</span>
foo();

<span class="hljs-keyword">let</span> a;
</code></pre>
<p>&#x5173;&#x4E8E;<em>&#x6682;&#x65F6;&#x6027;&#x6B7B;&#x533A;</em>&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x67E5;&#x770B;&#x8FD9;&#x91CC;<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let" target="_blank">Mozilla Developer Network</a>.</p>
<h2 id="&#x91CD;&#x5B9A;&#x4E49;&#x53CA;&#x5C4F;&#x853D;">&#x91CD;&#x5B9A;&#x4E49;&#x53CA;&#x5C4F;&#x853D;</h2>
<p>&#x6211;&#x4EEC;&#x63D0;&#x8FC7;&#x4F7F;&#x7528;<code>var</code>&#x58F0;&#x660E;&#x65F6;&#xFF0C;&#x5B83;&#x4E0D;&#x5728;&#x4E4E;&#x4F60;&#x58F0;&#x660E;&#x591A;&#x5C11;&#x6B21;&#xFF1B;&#x4F60;&#x53EA;&#x4F1A;&#x5F97;&#x5230;1&#x4E2A;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">x</span>) </span>{
    <span class="hljs-keyword">var</span> x;
    <span class="hljs-keyword">var</span> x;

    <span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">var</span> x;
    }
}
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x6240;&#x6709;<code>x</code>&#x7684;&#x58F0;&#x660E;&#x5B9E;&#x9645;&#x4E0A;&#x90FD;&#x5F15;&#x7528;&#x4E00;&#x4E2A;<em>&#x76F8;&#x540C;</em>&#x7684;<code>x</code>&#xFF0C;&#x5E76;&#x4E14;&#x8FD9;&#x662F;&#x5B8C;&#x5168;&#x6709;&#x6548;&#x7684;&#x4EE3;&#x7801;&#x3002;
&#x8FD9;&#x7ECF;&#x5E38;&#x4F1A;&#x6210;&#x4E3A;bug&#x7684;&#x6765;&#x6E90;&#x3002;
&#x597D;&#x7684;&#x662F;&#xFF0C;<code>let</code>&#x58F0;&#x660E;&#x5C31;&#x4E0D;&#x4F1A;&#x8FD9;&#x4E48;&#x5BBD;&#x677E;&#x4E86;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> x = <span class="hljs-number">20</span>; <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x4E0D;&#x80FD;&#x5728;1&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x91CC;&#x591A;&#x6B21;&#x58F0;&#x660E;`x`</span>
</code></pre>
<p>&#x5E76;&#x4E0D;&#x662F;&#x8981;&#x6C42;&#x4E24;&#x4E2A;&#x5747;&#x662F;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x58F0;&#x660E;TypeScript&#x624D;&#x4F1A;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x7684;&#x8B66;&#x544A;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">x</span>) </span>{
    <span class="hljs-keyword">let</span> x = <span class="hljs-number">100</span>; <span class="hljs-comment">// error: interferes with parameter declaration</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">g</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> x = <span class="hljs-number">100</span>;
    <span class="hljs-keyword">var</span> x = <span class="hljs-number">100</span>; <span class="hljs-comment">// error: can&apos;t have both declarations of &apos;x&apos;</span>
}
</code></pre>
<p>&#x5E76;&#x4E0D;&#x662F;&#x8BF4;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x4E0D;&#x80FD;&#x7528;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x6765;&#x58F0;&#x660E;&#x3002;
&#x800C;&#x662F;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x9700;&#x8981;&#x5728;&#x660E;&#x663E;&#x4E0D;&#x540C;&#x7684;&#x5757;&#x91CC;&#x58F0;&#x660E;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">condition, x</span>) </span>{
    <span class="hljs-keyword">if</span> (condition) {
        <span class="hljs-keyword">let</span> x = <span class="hljs-number">100</span>;
        <span class="hljs-keyword">return</span> x;
    }

    <span class="hljs-keyword">return</span> x;
}

f(<span class="hljs-literal">false</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// returns 0</span>
f(<span class="hljs-literal">true</span>, <span class="hljs-number">0</span>);  <span class="hljs-comment">// returns 100</span>
</code></pre>
<p>&#x5728;&#x4E00;&#x4E2A;&#x5D4C;&#x5957;&#x4F5C;&#x7528;&#x57DF;&#x91CC;&#x5F15;&#x5165;&#x4E00;&#x4E2A;&#x65B0;&#x540D;&#x5B57;&#x7684;&#x884C;&#x4E3A;&#x79F0;&#x505A;<em>&#x5C4F;&#x853D;</em>&#x3002;
&#x5B83;&#x662F;&#x4E00;&#x628A;&#x53CC;&#x5203;&#x5251;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x4F1A;&#x4E0D;&#x5C0F;&#x5FC3;&#x5730;&#x5F15;&#x5165;&#x65B0;&#x95EE;&#x9898;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x53EF;&#x80FD;&#x4F1A;&#x89E3;&#x51B3;&#x4E00;&#x4E9B;&#x9519;&#x8BEF;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x7528;<code>let</code>&#x91CD;&#x5199;&#x4E4B;&#x524D;&#x7684;<code>sumMatrix</code>&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sumMatrix</span>(<span class="hljs-params">matrix: <span class="hljs-built_in">number</span>[][]</span>) </span>{
    <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; matrix.length; i++) {
        <span class="hljs-keyword">var</span> currentRow = matrix[i];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; currentRow.length; i++) {
            sum += currentRow[i];
        }
    }

    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x7684;&#x5FAA;&#x73AF;&#x80FD;&#x5F97;&#x5230;&#x6B63;&#x786E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x56E0;&#x4E3A;&#x5185;&#x5C42;&#x5FAA;&#x73AF;&#x7684;<code>i</code>&#x53EF;&#x4EE5;&#x5C4F;&#x853D;&#x6389;&#x5916;&#x5C42;&#x5FAA;&#x73AF;&#x7684;<code>i</code>&#x3002;</p>
<p><em>&#x901A;&#x5E38;</em>&#x6765;&#x8BB2;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x4F7F;&#x7528;&#x5C4F;&#x853D;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5199;&#x51FA;&#x6E05;&#x6670;&#x7684;&#x4EE3;&#x7801;&#x3002;
&#x540C;&#x65F6;&#x4E5F;&#x6709;&#x4E9B;&#x573A;&#x666F;&#x9002;&#x5408;&#x5229;&#x7528;&#x5B83;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x597D;&#x597D;&#x6253;&#x7B97;&#x4E00;&#x4E0B;&#x3002;</p>
<h2 id="&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x7684;&#x83B7;&#x53D6;">&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x53D8;&#x91CF;&#x7684;&#x83B7;&#x53D6;</h2>
<p>&#x5728;&#x6211;&#x4EEC;&#x6700;&#x521D;&#x8C08;&#x53CA;&#x83B7;&#x53D6;&#x7528;<code>var</code>&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x7B80;&#x7565;&#x5730;&#x63A2;&#x7A76;&#x4E86;&#x4E00;&#x4E0B;&#x5728;&#x83B7;&#x53D6;&#x5230;&#x4E86;&#x53D8;&#x91CF;&#x4E4B;&#x540E;&#x5B83;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x600E;&#x6837;&#x7684;&#x3002;
&#x76F4;&#x89C2;&#x5730;&#x8BB2;&#xFF0C;&#x6BCF;&#x6B21;&#x8FDB;&#x5165;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x65F6;&#xFF0C;&#x5B83;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7684;<em>&#x73AF;&#x5883;</em>&#x3002;
&#x5C31;&#x7B97;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x73AF;&#x5883;&#x4E0E;&#x5176;&#x6355;&#x83B7;&#x7684;&#x53D8;&#x91CF;&#x4F9D;&#x7136;&#x5B58;&#x5728;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">theCityThatAlwaysSleeps</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> getCity;

    <span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">let</span> city = <span class="hljs-string">&quot;Seattle&quot;</span>;
        getCity = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">return</span> city;
        }
    }

    <span class="hljs-keyword">return</span> getCity();
}
</code></pre>
<p>&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5728;<code>city</code>&#x7684;&#x73AF;&#x5883;&#x91CC;&#x83B7;&#x53D6;&#x5230;&#x4E86;<code>city</code>&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x7B97;<code>if</code>&#x8BED;&#x53E5;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x540E;&#x6211;&#x4EEC;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5B83;&#x3002;</p>
<p>&#x56DE;&#x60F3;&#x4E00;&#x4E0B;&#x524D;&#x9762;<code>setTimeout</code>&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x6700;&#x540E;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x83B7;&#x53D6;&#x6BCF;&#x6B21;<code>for</code>&#x5FAA;&#x73AF;&#x8FED;&#x4EE3;&#x91CC;&#x7684;&#x72B6;&#x6001;&#x3002;
&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x505A;&#x7684;&#x662F;&#x4E3A;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x53D8;&#x91CF;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x53D8;&#x91CF;&#x73AF;&#x5883;&#x3002;
&#x8FD9;&#x6837;&#x505A;&#x633A;&#x75DB;&#x82E6;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;&#x4F60;&#x4E0D;&#x5FC5;&#x5728;TypeScript&#x91CC;&#x8FD9;&#x6837;&#x505A;&#x4E86;&#x3002;</p>
<p>&#x5F53;<code>let</code>&#x58F0;&#x660E;&#x51FA;&#x73B0;&#x5728;&#x5FAA;&#x73AF;&#x4F53;&#x91CC;&#x65F6;&#x62E5;&#x6709;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x7684;&#x884C;&#x4E3A;&#x3002;
&#x4E0D;&#x4EC5;&#x662F;&#x5728;&#x5FAA;&#x73AF;&#x91CC;&#x5F15;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x53D8;&#x91CF;&#x73AF;&#x5883;&#xFF0C;&#x800C;&#x662F;&#x9488;&#x5BF9;<em>&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;</em>&#x90FD;&#x4F1A;&#x521B;&#x5EFA;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x65B0;&#x4F5C;&#x7528;&#x57DF;&#x3002;
&#x8FD9;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x5728;&#x4F7F;&#x7528;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#x505A;&#x7684;&#x4E8B;&#xFF0C;&#x6240;&#x4EE5;&#x5728;<code>setTimeout</code>&#x4F8B;&#x5B50;&#x91CC;&#x6211;&#x4EEC;&#x4EC5;&#x4F7F;&#x7528;<code>let</code>&#x58F0;&#x660E;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span> ; i++) {
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{<span class="hljs-built_in">console</span>.log(i); }, <span class="hljs-number">100</span> * i);
}
</code></pre>
<p>&#x4F1A;&#x8F93;&#x51FA;&#x4E0E;&#x9884;&#x6599;&#x4E00;&#x81F4;&#x7684;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-text">0
1
2
3
4
5
6
7
8
9
</code></pre>
<h1 id="const-&#x58F0;&#x660E;"><code>const</code> &#x58F0;&#x660E;</h1>
<p><code>const</code> &#x58F0;&#x660E;&#x662F;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x7684;&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> numLivesForCat = <span class="hljs-number">9</span>;
</code></pre>
<p>&#x5B83;&#x4EEC;&#x4E0E;<code>let</code>&#x58F0;&#x660E;&#x76F8;&#x4F3C;&#xFF0C;&#x4F46;&#x662F;&#x5C31;&#x50CF;&#x5B83;&#x7684;&#x540D;&#x5B57;&#x6240;&#x8868;&#x8FBE;&#x7684;&#xFF0C;&#x5B83;&#x4EEC;&#x88AB;&#x8D4B;&#x503C;&#x540E;&#x4E0D;&#x80FD;&#x518D;&#x6539;&#x53D8;&#x3002;
&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x5B83;&#x4EEC;&#x62E5;&#x6709;&#x4E0E;<code>let</code>&#x76F8;&#x540C;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x89C4;&#x5219;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x5BF9;&#x5B83;&#x4EEC;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x8FD9;&#x5F88;&#x597D;&#x7406;&#x89E3;&#xFF0C;&#x5B83;&#x4EEC;&#x5F15;&#x7528;&#x7684;&#x503C;&#x662F;<em>&#x4E0D;&#x53EF;&#x53D8;&#x7684;</em>&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> numLivesForCat = <span class="hljs-number">9</span>;
<span class="hljs-keyword">const</span> kitty = {
    name: <span class="hljs-string">&quot;Aurora&quot;</span>,
    numLives: numLivesForCat,
}

<span class="hljs-comment">// Error</span>
kitty = {
    name: <span class="hljs-string">&quot;Danielle&quot;</span>,
    numLives: numLivesForCat
};

<span class="hljs-comment">// all &quot;okay&quot;</span>
kitty.name = <span class="hljs-string">&quot;Rory&quot;</span>;
kitty.name = <span class="hljs-string">&quot;Kitty&quot;</span>;
kitty.name = <span class="hljs-string">&quot;Cat&quot;</span>;
kitty.numLives--;
</code></pre>
<p>&#x9664;&#x975E;&#x4F60;&#x4F7F;&#x7528;&#x7279;&#x6B8A;&#x7684;&#x65B9;&#x6CD5;&#x53BB;&#x907F;&#x514D;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;<code>const</code>&#x53D8;&#x91CF;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x662F;&#x53EF;&#x4FEE;&#x6539;&#x7684;&#x3002;
&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;TypeScript&#x5141;&#x8BB8;&#x4F60;&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x8BBE;&#x7F6E;&#x6210;&#x53EA;&#x8BFB;&#x7684;&#x3002;
<a href="Interfaces.html">&#x63A5;&#x53E3;</a>&#x4E00;&#x7AE0;&#x6709;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x3002;</p>
<h1 id="let-vs-const"><code>let</code> vs. <code>const</code></h1>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6709;&#x4E24;&#x79CD;&#x4F5C;&#x7528;&#x57DF;&#x76F8;&#x4F3C;&#x7684;&#x58F0;&#x660E;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x81EA;&#x7136;&#x4F1A;&#x95EE;&#x5230;&#x5E95;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;&#x54EA;&#x4E2A;&#x3002;
&#x4E0E;&#x5927;&#x591A;&#x6570;&#x6CDB;&#x6CDB;&#x7684;&#x95EE;&#x9898;&#x4E00;&#x6837;&#xFF0C;&#x7B54;&#x6848;&#x662F;&#xFF1A;&#x4F9D;&#x60C5;&#x51B5;&#x800C;&#x5B9A;&#x3002;</p>
<p>&#x4F7F;&#x7528;<a href="https://en.wikipedia.org/wiki/Principle_of_least_privilege" target="_blank">&#x6700;&#x5C0F;&#x7279;&#x6743;&#x539F;&#x5219;</a>&#xFF0C;&#x6240;&#x6709;&#x53D8;&#x91CF;&#x9664;&#x4E86;&#x4F60;&#x8BA1;&#x5212;&#x53BB;&#x4FEE;&#x6539;&#x7684;&#x90FD;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;<code>const</code>&#x3002;
&#x57FA;&#x672C;&#x539F;&#x5219;&#x5C31;&#x662F;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x4E0D;&#x9700;&#x8981;&#x5BF9;&#x5B83;&#x5199;&#x5165;&#xFF0C;&#x90A3;&#x4E48;&#x5176;&#x5B83;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x4EE3;&#x7801;&#x7684;&#x4EBA;&#x4E5F;&#x4E0D;&#x80FD;&#x591F;&#x5199;&#x5165;&#x5B83;&#x4EEC;&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x601D;&#x8003;&#x4E3A;&#x4EC0;&#x4E48;&#x4F1A;&#x9700;&#x8981;&#x5BF9;&#x8FD9;&#x4E9B;&#x53D8;&#x91CF;&#x91CD;&#x65B0;&#x8D4B;&#x503C;&#x3002;
&#x4F7F;&#x7528;<code>const</code>&#x4E5F;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x66F4;&#x5BB9;&#x6613;&#x7684;&#x63A8;&#x6D4B;&#x6570;&#x636E;&#x7684;&#x6D41;&#x52A8;&#x3002;</p>
<p>&#x8DDF;&#x636E;&#x4F60;&#x7684;&#x81EA;&#x5DF1;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x5408;&#x9002;&#x7684;&#x8BDD;&#xFF0C;&#x4E0E;&#x56E2;&#x961F;&#x6210;&#x5458;&#x5546;&#x8BAE;&#x4E00;&#x4E0B;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x624B;&#x518C;&#x5927;&#x90E8;&#x5206;&#x5730;&#x65B9;&#x90FD;&#x4F7F;&#x7528;&#x4E86;<code>let</code>&#x58F0;&#x660E;&#x3002;</p>
<h1 id="&#x89E3;&#x6784;">&#x89E3;&#x6784;</h1>
<p>Another TypeScript&#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x89E3;&#x6790;&#x5176;&#x5B83; ECMAScript 2015 &#x7279;&#x6027;&#x4E86;&#x3002;
&#x5B8C;&#x6574;&#x5217;&#x8868;&#x8BF7;&#x53C2;&#x89C1; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" target="_blank">the article on the Mozilla Developer Network</a>&#x3002;
&#x672C;&#x7AE0;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x7B80;&#x77ED;&#x7684;&#x6982;&#x8FF0;&#x3002;</p>
<h2 id="&#x89E3;&#x6784;&#x6570;&#x7EC4;">&#x89E3;&#x6784;&#x6570;&#x7EC4;</h2>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x89E3;&#x6784;&#x83AB;&#x8FC7;&#x4E8E;&#x6570;&#x7EC4;&#x7684;&#x89E3;&#x6784;&#x8D4B;&#x503C;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> input = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];
<span class="hljs-keyword">let</span> [first, second] = input;
<span class="hljs-built_in">console</span>.log(first); <span class="hljs-comment">// outputs 1</span>
<span class="hljs-built_in">console</span>.log(second); <span class="hljs-comment">// outputs 2</span>
</code></pre>
<p>&#x8FD9;&#x521B;&#x5EFA;&#x4E86;2&#x4E2A;&#x547D;&#x540D;&#x53D8;&#x91CF; <code>first</code> &#x548C; <code>second</code>&#x3002;
&#x76F8;&#x5F53;&#x4E8E;&#x4F7F;&#x7528;&#x4E86;&#x7D22;&#x5F15;&#xFF0C;&#x4F46;&#x66F4;&#x4E3A;&#x65B9;&#x4FBF;&#xFF1A;</p>
<pre><code class="lang-ts">first = input[<span class="hljs-number">0</span>];
second = input[<span class="hljs-number">1</span>];
</code></pre>
<p>&#x89E3;&#x6784;&#x4F5C;&#x7528;&#x4E8E;&#x5DF2;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x4F1A;&#x66F4;&#x597D;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// swap variables</span>
[first, second] = [second, first];
</code></pre>
<p>&#x4F5C;&#x7528;&#x4E8E;&#x51FD;&#x6570;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">[first, second]: [<span class="hljs-built_in">number</span>, <span class="hljs-built_in">number</span>]</span>) </span>{
    <span class="hljs-built_in">console</span>.log(first);
    <span class="hljs-built_in">console</span>.log(second);
}
f(input);
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6570;&#x7EC4;&#x91CC;&#x4F7F;&#x7528;<code>...</code>&#x8BED;&#x6CD5;&#x521B;&#x5EFA;&#x5269;&#x4F59;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> [first, ...rest] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-built_in">console</span>.log(first); <span class="hljs-comment">// outputs 1</span>
<span class="hljs-built_in">console</span>.log(rest); <span class="hljs-comment">// outputs [ 2, 3, 4 ]</span>
</code></pre>
<p>&#x5F53;&#x7136;&#xFF0C;&#x7531;&#x4E8E;&#x662F;JavaScript, &#x4F60;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x4F60;&#x4E0D;&#x5173;&#x5FC3;&#x7684;&#x5C3E;&#x968F;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> [first] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-built_in">console</span>.log(first); <span class="hljs-comment">// outputs 1</span>
</code></pre>
<p>&#x6216;&#x5176;&#x5B83;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> [, second, , fourth] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
</code></pre>
<h2 id="&#x5BF9;&#x8C61;&#x89E3;&#x6784;">&#x5BF9;&#x8C61;&#x89E3;&#x6784;</h2>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x89E3;&#x6784;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> o = {
    a: <span class="hljs-string">&quot;foo&quot;</span>,
    b: <span class="hljs-number">12</span>,
    c: <span class="hljs-string">&quot;bar&quot;</span>
};
<span class="hljs-keyword">let</span> { a, b } = o;
</code></pre>
<p>&#x8FD9;&#x901A;&#x8FC7; <code>o.a</code> and <code>o.b</code> &#x521B;&#x5EFA;&#x4E86; <code>a</code> &#x548C; <code>b</code> &#x3002;
&#x6CE8;&#x610F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x9700;&#x8981; <code>c</code> &#x4F60;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x5B83;&#x3002;</p>
<p>&#x5C31;&#x50CF;&#x6570;&#x7EC4;&#x89E3;&#x6784;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x6CA1;&#x6709;&#x58F0;&#x660E;&#x7684;&#x8D4B;&#x503C;&#xFF1A;</p>
<pre><code class="lang-ts">({ a, b } = { a: <span class="hljs-string">&quot;baz&quot;</span>, b: <span class="hljs-number">101</span> });
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7528;&#x62EC;&#x53F7;&#x5C06;&#x5B83;&#x62EC;&#x8D77;&#x6765;&#xFF0C;&#x56E0;&#x4E3A;Javascript&#x901A;&#x5E38;&#x4F1A;&#x5C06;&#x4EE5; <code>{</code> &#x8D77;&#x59CB;&#x7684;&#x8BED;&#x53E5;&#x89E3;&#x6790;&#x4E3A;&#x4E00;&#x4E2A;&#x5757;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x5BF9;&#x8C61;&#x91CC;&#x4F7F;&#x7528;<code>...</code>&#x8BED;&#x6CD5;&#x521B;&#x5EFA;&#x5269;&#x4F59;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> { a, ...passthrough } = o;
<span class="hljs-keyword">let</span> total = passthrough.b + passthrough.c.length;
</code></pre>
<h3 id="&#x5C5E;&#x6027;&#x91CD;&#x547D;&#x540D;">&#x5C5E;&#x6027;&#x91CD;&#x547D;&#x540D;</h3>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x7ED9;&#x5C5E;&#x6027;&#x4EE5;&#x4E0D;&#x540C;&#x7684;&#x540D;&#x5B57;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> { a: newName1, b: newName2 } = o;
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;&#x8BED;&#x6CD5;&#x5F00;&#x59CB;&#x53D8;&#x5F97;&#x6DF7;&#x4E71;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x5C06; <code>a: newName1</code> &#x8BFB;&#x505A; &quot;<code>a</code> &#x4F5C;&#x4E3A; <code>newName1</code>&quot;&#x3002;
&#x65B9;&#x5411;&#x662F;&#x4ECE;&#x5DE6;&#x5230;&#x53F3;&#xFF0C;&#x597D;&#x50CF;&#x4F60;&#x5199;&#x6210;&#x4E86;&#x4EE5;&#x4E0B;&#x6837;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> newName1 = o.a;
<span class="hljs-keyword">let</span> newName2 = o.b;
</code></pre>
<p>&#x4EE4;&#x4EBA;&#x56F0;&#x60D1;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x5192;&#x53F7;<em>&#x4E0D;&#x662F;</em>&#x6307;&#x793A;&#x7C7B;&#x578B;&#x7684;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x60F3;&#x6307;&#x5B9A;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#xFF0C; &#x4ECD;&#x7136;&#x9700;&#x8981;&#x5728;&#x5176;&#x540E;&#x5199;&#x4E0A;&#x5B8C;&#x6574;&#x7684;&#x6A21;&#x5F0F;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> {a, b}: {a: <span class="hljs-built_in">string</span>, b: <span class="hljs-built_in">number</span>} = o;
</code></pre>
<h3 id="&#x9ED8;&#x8BA4;&#x503C;">&#x9ED8;&#x8BA4;&#x503C;</h3>
<p>&#x9ED8;&#x8BA4;&#x503C;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x5728;&#x5C5E;&#x6027;&#x4E3A; undefined &#x65F6;&#x4F7F;&#x7528;&#x7F3A;&#x7701;&#x503C;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">keepWholeObject</span>(<span class="hljs-params">wholeObject: { a: <span class="hljs-built_in">string</span>, b?: <span class="hljs-built_in">number</span> }</span>) </span>{
    <span class="hljs-keyword">let</span> { a, b = <span class="hljs-number">1001</span> } = wholeObject;
}
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5373;&#x4F7F; <code>b</code> &#x4E3A; undefined &#xFF0C; <code>keepWholeObject</code> &#x51FD;&#x6570;&#x7684;&#x53D8;&#x91CF; <code>wholeObject</code> &#x7684;&#x5C5E;&#x6027; <code>a</code> &#x548C; <code>b</code> &#x90FD;&#x4F1A;&#x6709;&#x503C;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x58F0;&#x660E;">&#x51FD;&#x6570;&#x58F0;&#x660E;</h2>
<p>&#x89E3;&#x6784;&#x4E5F;&#x80FD;&#x7528;&#x4E8E;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x3002;
&#x770B;&#x4EE5;&#x4E0B;&#x7B80;&#x5355;&#x7684;&#x60C5;&#x51B5;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> C = { a: <span class="hljs-built_in">string</span>, b?: <span class="hljs-built_in">number</span> }
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">{ a, b }: C</span>): <span class="hljs-title">void</span> </span>{
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x4F46;&#x662F;&#xFF0C;&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x66F4;&#x591A;&#x7684;&#x662F;&#x6307;&#x5B9A;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x89E3;&#x6784;&#x9ED8;&#x8BA4;&#x503C;&#x6709;&#x4E9B;&#x68D8;&#x624B;&#x3002;
&#x9996;&#x5148;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x5728;&#x9ED8;&#x8BA4;&#x503C;&#x4E4B;&#x524D;&#x8BBE;&#x7F6E;&#x5176;&#x683C;&#x5F0F;&#x3002;</p>
<pre><code class="lang-ts">function f({ a=&quot;&quot;, b=0 } = {}): void {
    // ...
}
f();
</code></pre>
<blockquote>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5C06;&#x5728;&#x672C;&#x624B;&#x518C;&#x540E;&#x6587;&#x4ECB;&#x7ECD;&#x3002;</p>
</blockquote>
<p>&#x5176;&#x6B21;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x77E5;&#x9053;&#x5728;&#x89E3;&#x6784;&#x5C5E;&#x6027;&#x4E0A;&#x7ED9;&#x4E88;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x6216;&#x53EF;&#x9009;&#x7684;&#x5C5E;&#x6027;&#x7528;&#x6765;&#x66FF;&#x6362;&#x4E3B;&#x521D;&#x59CB;&#x5316;&#x5217;&#x8868;&#x3002;
&#x8981;&#x77E5;&#x9053; <code>C</code> &#x7684;&#x5B9A;&#x4E49;&#x6709;&#x4E00;&#x4E2A; <code>b</code> &#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-ts">function f({ a, b = 0 } = { a: &quot;&quot; }): void {
    // ...
}
f({ a: &quot;yes&quot; }); // ok, default b = 0
f(); // ok, default to {a: &quot;&quot;}, which then defaults b = 0
f({}); // error, &apos;a&apos; is required if you supply an argument
</code></pre>
<p>&#x8981;&#x5C0F;&#x5FC3;&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x3002;
&#x4ECE;&#x524D;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x5C31;&#x7B97;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x662F;&#x96BE;&#x4EE5;&#x7406;&#x89E3;&#x7684;&#x3002;
&#x5C24;&#x5176;&#x5F53;&#x5B58;&#x5728;&#x6DF1;&#x5C42;&#x5D4C;&#x5957;&#x89E3;&#x6784;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x7B97;&#x8FD9;&#x65F6;&#x6CA1;&#x6709;&#x5806;&#x53E0;&#x5728;&#x4E00;&#x8D77;&#x7684;&#x91CD;&#x547D;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x548C;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#xFF0C;&#x4E5F;&#x662F;&#x4EE4;&#x4EBA;&#x96BE;&#x4EE5;&#x7406;&#x89E3;&#x7684;&#x3002;
&#x89E3;&#x6784;&#x8868;&#x8FBE;&#x5F0F;&#x8981;&#x5C3D;&#x91CF;&#x4FDD;&#x6301;&#x5C0F;&#x800C;&#x7B80;&#x5355;&#x3002;
&#x4F60;&#x81EA;&#x5DF1;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x89E3;&#x6784;&#x5C06;&#x4F1A;&#x751F;&#x6210;&#x7684;&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<h2 id="&#x5C55;&#x5F00;">&#x5C55;&#x5F00;</h2>
<p>&#x5C55;&#x5F00;&#x64CD;&#x4F5C;&#x7B26;&#x6B63;&#x4E0E;&#x89E3;&#x6784;&#x76F8;&#x53CD;&#x3002;
&#x5B83;&#x5141;&#x8BB8;&#x4F60;&#x5C06;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5C55;&#x5F00;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x6216;&#x5C06;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5C55;&#x5F00;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;
&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> first = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];
<span class="hljs-keyword">let</span> second = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">let</span> bothPlus = [<span class="hljs-number">0</span>, ...first, ...second, <span class="hljs-number">5</span>];
</code></pre>
<p>&#x8FD9;&#x4F1A;&#x4EE4;<code>bothPlus</code>&#x7684;&#x503C;&#x4E3A;<code>[0, 1, 2, 3, 4, 5]</code>&#x3002;
&#x5C55;&#x5F00;&#x64CD;&#x4F5C;&#x521B;&#x5EFA;&#x4E86;<code>first</code>&#x548C;<code>second</code>&#x7684;&#x4E00;&#x4EFD;&#x6D45;&#x62F7;&#x8D1D;&#x3002;
&#x5B83;&#x4EEC;&#x4E0D;&#x4F1A;&#x88AB;&#x5C55;&#x5F00;&#x64CD;&#x4F5C;&#x6240;&#x6539;&#x53D8;&#x3002;</p>
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x5C55;&#x5F00;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> defaults = { food: <span class="hljs-string">&quot;spicy&quot;</span>, price: <span class="hljs-string">&quot;$$&quot;</span>, ambiance: <span class="hljs-string">&quot;noisy&quot;</span> };
<span class="hljs-keyword">let</span> search = { ...defaults, food: <span class="hljs-string">&quot;rich&quot;</span> };
</code></pre>
<p><code>search</code>&#x7684;&#x503C;&#x4E3A;<code>{ food: &quot;rich&quot;, price: &quot;$$&quot;, ambiance: &quot;noisy&quot; }</code>&#x3002;
&#x5BF9;&#x8C61;&#x7684;&#x5C55;&#x5F00;&#x6BD4;&#x6570;&#x7EC4;&#x7684;&#x5C55;&#x5F00;&#x8981;&#x590D;&#x6742;&#x7684;&#x591A;&#x3002;
&#x50CF;&#x6570;&#x7EC4;&#x5C55;&#x5F00;&#x4E00;&#x6837;&#xFF0C;&#x5B83;&#x662F;&#x4ECE;&#x5DE6;&#x81F3;&#x53F3;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x4F46;&#x7ED3;&#x679C;&#x4ECD;&#x4E3A;&#x5BF9;&#x8C61;&#x3002;
&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x51FA;&#x73B0;&#x5728;&#x5C55;&#x5F00;&#x5BF9;&#x8C61;&#x540E;&#x9762;&#x7684;&#x5C5E;&#x6027;&#x4F1A;&#x8986;&#x76D6;&#x524D;&#x9762;&#x7684;&#x5C5E;&#x6027;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4FEE;&#x6539;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5728;&#x7ED3;&#x5C3E;&#x5904;&#x8FDB;&#x884C;&#x5C55;&#x5F00;&#x7684;&#x8BDD;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> defaults = { food: <span class="hljs-string">&quot;spicy&quot;</span>, price: <span class="hljs-string">&quot;$$&quot;</span>, ambiance: <span class="hljs-string">&quot;noisy&quot;</span> };
<span class="hljs-keyword">let</span> search = { food: <span class="hljs-string">&quot;rich&quot;</span>, ...defaults };
</code></pre>
<p>&#x90A3;&#x4E48;&#xFF0C;<code>defaults</code>&#x91CC;&#x7684;<code>food</code>&#x5C5E;&#x6027;&#x4F1A;&#x91CD;&#x5199;<code>food: &quot;rich&quot;</code>&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x5BF9;&#x8C61;&#x5C55;&#x5F00;&#x8FD8;&#x6709;&#x5176;&#x5B83;&#x4E00;&#x4E9B;&#x610F;&#x60F3;&#x4E0D;&#x5230;&#x7684;&#x9650;&#x5236;&#x3002;
&#x9996;&#x5148;&#xFF0C;&#x5B83;&#x4EC5;&#x5305;&#x542B;&#x5BF9;&#x8C61;
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties" target="_blank">&#x81EA;&#x8EAB;&#x7684;&#x53EF;&#x679A;&#x4E3E;&#x5C5E;&#x6027;</a>&#x3002;
&#x5927;&#x4F53;&#x4E0A;&#x662F;&#x8BF4;&#x5F53;&#x4F60;&#x5C55;&#x5F00;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5B9E;&#x4F8B;&#x65F6;&#xFF0C;&#x4F60;&#x4F1A;&#x4E22;&#x5931;&#x5176;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> C {
  p = <span class="hljs-number">12</span>;
  m() {
  }
}
<span class="hljs-keyword">let</span> c = <span class="hljs-keyword">new</span> C();
<span class="hljs-keyword">let</span> clone = { ...c };
clone.p; <span class="hljs-comment">// ok</span>
clone.m(); <span class="hljs-comment">// error!</span>
</code></pre>
<p>&#x5176;&#x6B21;&#xFF0C;TypeScript&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x5141;&#x8BB8;&#x5C55;&#x5F00;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x4E0A;&#x7684;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x3002;
&#x8FD9;&#x4E2A;&#x7279;&#x6027;&#x4F1A;&#x5728;TypeScript&#x7684;&#x672A;&#x6765;&#x7248;&#x672C;&#x4E2D;&#x8003;&#x8651;&#x5B9E;&#x73B0;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Basic Types.html" class="navigation navigation-prev " aria-label="Previous page: 基础类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Interfaces.html" class="navigation navigation-next " aria-label="Next page: 接口">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"变量声明","level":"1.3.2","depth":2,"next":{"title":"接口","level":"1.3.3","depth":2,"path":"doc/handbook/Interfaces.md","ref":"./doc/handbook/Interfaces.md","articles":[]},"previous":{"title":"基础类型","level":"1.3.1","depth":2,"path":"doc/handbook/Basic Types.md","ref":"./doc/handbook/Basic Types.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/handbook/Variable Declarations.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

